package cn.dylan.org.service.impl;

import cn.dylan.basic.service.impl.BaseServiceImpl;
import cn.dylan.basic.util.Md5Utils;
import cn.dylan.basic.util.PageList;
import cn.dylan.basic.util.StrUtils;
import cn.dylan.org.domain.Employee;
import cn.dylan.org.mapper.EmployeeMapper;
import cn.dylan.org.query.EmployeeQuery;
import cn.dylan.org.service.IEmployeeService;
import cn.dylan.user.domain.Logininfo;
import cn.dylan.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class EmployeeServiceImpl extends
        BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void delete(Long id) {
        Employee employee = employeeMapper.LoadById(id);
        //删除登录信息表数据
        logininfoMapper.delete(employee.getLogininfo_id());

        //删除t_employee:删除中间表信息：t_employee_role
        employeeMapper.removeEmployeeRoleByEmployeeId(id);

        //删除employee表数据
        super.delete(id);
    }

    @Override
    public void save(Employee employee) {
        //super.save(employee);
        // 拿到一个盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 对盐值和密码加密
        String password = Md5Utils.encrypByMd5(salt.toString() + employee.getPassword());
        // 存入employee
        employee.setSalt(salt);
        employee.setPassword(password);
        // 同步到logininfo
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);

        employee.setLogininfo_id(logininfo.getId());

        employeeMapper.save(employee);// 同 super.save(employee)
        // 保存中间表信息
        employeeMapper.saveEmployeeRole(employee);

    }


    @Override
    public void update(Employee employee) {
        //1.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = employee2Logininfo(employee);
        Long logininfo_id = employeeMapper.LoadById(employee.getId()).getLogininfo_id();
        logininfo.setId(logininfo_id);

        logininfoMapper.update(logininfo);
        //登录信息id和店铺信息id不能修改
        super.update(employee);

        //删除中间表信息
        employeeMapper.removeEmployeeRoleByEmployeeId(employee.getId());
        //保存新的信息
        employeeMapper.saveEmployeeRole(employee);
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    @Override
    public List<Employee> getShopEmployees(Long shopId) {
        return employeeMapper.getShopEmployees(shopId);
    }
}
